package com.munhrasoft.elearning.business.logic;

import java.util.List;
import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.munhrasoft.elearning.business.entities.Students;

@Stateless
public class StudentsEJB {

    private static Logger logger = Logger.getLogger(StudentsEJB.class.getName());

    @PersistenceContext(unitName = "elearningPU")
    private EntityManager em;

    @SuppressWarnings("unchecked")
    public List<Students> findStudentsByName(String name) {
        Query query = em.createNamedQuery("findStudentsByName");
        query.setParameter("name", name.toLowerCase() + "%");
        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<Students> findAllStudents() {
        Query query = em.createNamedQuery("findAllStudents");
        return query.getResultList();
    }

    /*
    private Students persistCorrectAnswer(Students student, Long idCourse, Questions question) {

        if (student.getCorrectQuestions().get(idCourse) == null) {
            List<Questions> questions = new ArrayList<Questions>();
            questions.add(question);
            student.getCorrectQuestions().put(idCourse, questions);
        } else {
            student.getCorrectQuestions().get(idCourse).add(question);
        }

        return student;
    }

    private Students persistWrongAnswer(Students student, Long idCourse, Questions question) {

        if (student.getWrongQuestions().get(idCourse) == null) {
            List<Questions> questions = new ArrayList<Questions>();
            questions.add(question);
            student.getWrongQuestions().put(idCourse, questions);
        } else {
            student.getWrongQuestions().get(idCourse).add(question);
        }

        return student;

    }

    public void persistAnsweredQuestion(Students student, Long idCourse, Questions question) {

        Students updateStudent;

        if (question.isCorrect()) {
            updateStudent = persistCorrectAnswer(student, idCourse, question);
        } else {
            updateStudent = persistWrongAnswer(student, idCourse, question);
        }

        em.persist(updateStudent);
    }
    */

}
